package com.cgtong.android.mvp.model;

import com.blankj.utilcode.util.ToastUtils;
import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.bean.SimpleResult;
import com.cgtong.android.http.ApiManager;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.http.callback.FollowCallBack;
import com.cgtong.android.http.callback.ProblemDetailCallBack;
import com.cgtong.android.mvp.contract.ProblemDetailContract;
import com.google.gson.Gson;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by chunc on 2017/10/24.
 */

public class ProblemDetailModel implements ProblemDetailContract.Model {
    @Override
    public void httpProblemDetailData(long problemId, final ProblemDetailCallBack<ProblemBean> simpleCallBack) {
        EasyHttp.post(ApiManager.PROBLEM_DETAIL_URL)
                .params("problemid", String.valueOf(problemId))//支持添加多个参数同时添加
                .accessToken(true)//本次请求是否追加token
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        simpleCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(String response) {
                        try {
                            JSONObject json = new JSONObject(response);
                            JSONObject jsonObject = json.optJSONObject("data");
                            ProblemBean problemBean = new ProblemBean();
                            problemBean.setCode(json.optInt("code"));
                            problemBean.setMsg(json.optString("msg"));
                            if (jsonObject != null) {
                                ProblemBean problem = new Gson().fromJson(jsonObject.toString(), ProblemBean.class);
                                simpleCallBack.onSuccess(problem);
                                return;
                            }
                            simpleCallBack.onError(problemBean);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });


    }

    @Override
    public void follow(final long id, final FollowCallBack<ApiResult> followCallBack) {
        EasyHttp.post(ApiManager.PROBLEM_FOLLOW_PROBLEM_URL)
                .params("problemid", String.valueOf(id))//支持添加多个参数同时添加
                .accessToken(true)//本次请求是否追加token
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        followCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(String apiResult) {
                        try {
                            JSONObject object = new JSONObject(apiResult);
                            ApiResult result = new ApiResult();
                            result.setCode(object.optInt("code"));
                            result.setMsg(object.optString("msg"));
                            followCallBack.onFollowUserSuccess(id, result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void evaluate(long problemId, int level, final SimpleCallBack<SimpleResult> simpleCallBack) {
        EasyHttp.post(ApiManager.PROBLEM_REVIEW_PROBLEM_URL)
                .params("problemid", String.valueOf(problemId))
                .params("level", String.valueOf(level))
                .accessToken(true)//本次请求是否追加token
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showShort(e.getMessage());
                    }

                    @Override
                    public void onSuccess(String s) {
                        try {
                            JSONObject object = new JSONObject(s);
                            SimpleResult result = new SimpleResult();
                            result.setCode(object.optInt("code"));
                            result.setMsg(object.optString("msg"));
                            simpleCallBack.onSuccess(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void deleteReport(final long id, final BaseCallBack<ApiResult> baseCallBack) {
        EasyHttp.post(ApiManager.MINE_DELETE_MY_REPORTS_URL)
                .params("problemId", String.valueOf(id))//支持添加多个参数同时添加
                .accessToken(true)//本次请求是否追加token
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        baseCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(String apiResult) {
                        try {
                            JSONObject object = new JSONObject(apiResult);
                            ApiResult result = new ApiResult();
                            result.setCode(object.optInt("code"));
                            result.setMsg(object.optString("msg"));
                            baseCallBack.onSuccess(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
